package com.csun.cmny.provider.controller.hualong;

import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.dto.hualong.HuaLongRosterDto;
import com.csun.cmny.provider.model.dto.hualong.HuaLongRosterQueryDto;
import com.csun.cmny.provider.model.dto.hualong.HuaLongRosterShiftDto;
import com.csun.cmny.provider.model.vo.hualong.HuaLongRosterVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcHuaLongRosterItemService;
import com.csun.cmny.provider.service.OmcHuaLongRosterService;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2020-10-29 11:53
 * @Description:
 */
@RestController
@RequestMapping("/hualong/roster")
public class OmcHuaLongRosterController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private OmcHuaLongRosterService omcHuaLongRosterService;
    @Resource
    private OmcHuaLongRosterItemService omcHuaLongRosterItemService;

    /**
     * 主管排班
     */
    @PostMapping(value = "/save")
    public Wrapper save(@RequestBody HuaLongRosterDto huaLongRosterDto) {

        // TODO 是否为主管

        Integer result = omcHuaLongRosterService.save(huaLongRosterDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                result);
    }

    /**
     * 查询主管排班列表
     */
    @PostMapping(value = "/queryListWithPage")
    public Wrapper queryListWithPage(@RequestBody HuaLongRosterQueryDto huaLongRosterQueryDto) {

        huaLongRosterQueryDto.setGroupId(getLoginAuthDto().getGroupId());

        List<HuaLongRosterVo> huaLongRosterVoList = omcHuaLongRosterService.queryListWithPage(huaLongRosterQueryDto);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                new PageInfo<>(huaLongRosterVoList));
    }

    /**
     * 根据Id查看排班详情
     */
    @PostMapping(value = "/queryDetailById/{id}")
    public Wrapper queryDetailById(@PathVariable Long id) {

        HuaLongRosterVo huaLongRosterVo = omcHuaLongRosterService.queryDetailById(id);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                huaLongRosterVo);
    }

    /**
     * 根据Id删除排班
     */
    @PostMapping(value = "/deleteById/{id}")
    public Wrapper deleteById(@PathVariable Long id) {

        Integer result = omcHuaLongRosterService.deleteById(id);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                result);
    }


    /**
     * 主管调班
     */
    @PostMapping(value = "/change")
    public Wrapper change(@RequestBody HuaLongRosterDto huaLongRosterDto) {

        // TODO 是否为主管

        Integer result = omcHuaLongRosterService.change(huaLongRosterDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                result);
    }

    /**
     * 修改排班项
     */
    @PostMapping(value = "/changeById")
    public Wrapper changeById(@RequestBody HuaLongRosterShiftDto huaLongRosterShiftDto) {

        // TODO 是否为主管

        Integer result = omcHuaLongRosterItemService.changeById(huaLongRosterShiftDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                result);
    }

}
